<?php
/**
 * FoxyCart Test XML Generator
 * 
 * @link http://wiki.foxycart.com/integration:misc:test_xml_post
 * @version 0.6a
 */
/*
	DESCRIPTION: =================================================================
	The purpose of this file is to help you set up and debug your FoxyCart XML DataFeed scripts.
	It's designed to mimic FoxyCart.com and send encrypted and encoded XML to a URL of your choice.
	It will print out the response that your script gives back, which should be "foxy" if successful.
	
	USAGE: =======================================================================
	- Place this file somewhere on your server.
	- Edit the $myURL to the URL where your XML processing script is located.
	- Edit the $myKey to match the key you put in your FoxyCart admin.
	- Edit the $XMLOutput if you have specific data you'd like to test.
	- Save.
	- Load this file in your browser. It will send XML to your script just like FoxyCart would
	  after an order on your store, and will output what your script returns.
	- Test until you get your script working properly.
	
	REQUIREMENTS: ================================================================
	- PHP
	- cURL support in PHP
*/

// ======================================================================================
// CHANGE THIS DATA:
// Set the URL you want to post the XML to.
// Set the key you entered in your FoxyCart.com admin.
// Modify the XML below as necessary.  DO NOT modify the structure, just the data
// ======================================================================================
include_once 'config.inc.php';

$myURL = 'http://localhost/path/to/datafeed.php'
$myKey = $XML_KEY;

// You can change the test data below if you'd like to test specific fields.
// For example, you may want to set it up to mirror 
$XMLOutput = <<<XML
<?xml version='1.0' standalone='yes'?>
<foxydata>
<datafeed_version>XML FoxyCart Version 0.6</datafeed_version>
<transactions>
 <transaction>
  <id>70122</id>
  <transaction_date>2008-07-28 13:53:20</transaction_date>
  <customer_id>11205</customer_id>
  <customer_first_name>Dirk</customer_first_name>
  <customer_last_name>Gently</customer_last_name>
  <customer_address1>1024 Kilobyte Way</customer_address1>
  <customer_address2></customer_address2>
  <customer_city>Turing</customer_city>
  <customer_state>TN</customer_state>
  <customer_postal_code>16384</customer_postal_code>
  <customer_country>US</customer_country>
  <customer_phone>555-555-5555</customer_phone>
  <customer_email>dirk@holistic-detective-agency.com</customer_email>
  <customer_ip>24.16.251.185</customer_ip>
  <shipping_first_name></shipping_first_name>
  <shipping_last_name></shipping_last_name>
  <shipping_address1></shipping_address1>
  <shipping_address2></shipping_address2>
  <shipping_city></shipping_city>
  <shipping_state></shipping_state>
  <shipping_postal_code></shipping_postal_code>
  <shipping_country></shipping_country>
  <shipping_phone></shipping_phone>
  <shipping_service_description></shipping_service_description>
  <purchase_order></purchase_order>
  <product_total>0.00</product_total>
  <tax_total>0.00</tax_total>
  <shipping_total>0.00</shipping_total>
  <order_total>0.00</order_total>
  <customer_password>098f6bcd4621d373cade4e832627b4f6</customer_password>
  <custom_fields>
   <custom_field>
    <custom_field_name>apply</custom_field_name>
    <custom_field_value>Continue â</custom_field_value>
   </custom_field>
  </custom_fields>
  <transaction_details>
   <transaction_detail>
    <product_name>Reseller Application</product_name>
    <product_price>0.00</product_price>
    <product_quantity>1</product_quantity>
    <product_weight>0.000</product_weight>
    <product_code></product_code>
    <subscription_frequency></subscription_frequency>
    <subscription_startdate>0000-00-00</subscription_startdate>
    <next_transaction_date>0000-00-00</next_transaction_date>
    <shipto>Me</shipto>
    <category_description>Reseller Application</category_description>
    <category_code>application</category_code>
    <product_delivery_type>notshipped</product_delivery_type>
    <transaction_detail_options>
     <transaction_detail_option>
      <product_option_name>website</product_option_name>
      <product_option_value>http://themancan.com</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
     <transaction_detail_option>
      <product_option_name>revenue</product_option_name>
      <product_option_value>0-$100k</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
     <transaction_detail_option>
      <product_option_name>reseller_type</product_option_name>
      <product_option_value>Online store</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
     <transaction_detail_option>
      <product_option_name>contact_name</product_option_name>
      <product_option_value>Dirk Gently</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
     <transaction_detail_option>
      <product_option_name>contact_email</product_option_name>
      <product_option_value>dirk@holistic-detective-agency.com</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
     <transaction_detail_option>
      <product_option_name>contact_phone</product_option_name>
      <product_option_value>555-555-5555</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
     <transaction_detail_option>
      <product_option_name>application_type</product_option_name>
      <product_option_value>reseller</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
     <transaction_detail_option>
      <product_option_name>terms</product_option_name>
      <product_option_value>on</product_option_value>
      <price_mod>0.00</price_mod>
      <weight_mod>0.000</weight_mod>
     </transaction_detail_option>
    </transaction_detail_options>
   </transaction_detail>
  </transaction_details>
 </transaction>
</transactions>
</foxydata>
XML;



// ======================================================================================
// YOU'RE DONE.  DO NOT MODIFY BELOW THIS LINE.
// The code below this line should not be modified unless you have a good reason to do so.
// ======================================================================================

// ======================================================================================
// ENCRYPT YOUR XML
// Modify the include path to go to the rc4crypt file.
// ======================================================================================
$XMLOutput_encrypted = rc4crypt::encrypt($myKey,$XMLOutput);
$XMLOutput_encrypted = urlencode($XMLOutput_encrypted);


// ======================================================================================
// POST YOUR XML TO YOUR SITE
// Do not modify.
// ======================================================================================
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $myURL);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);


header("content-type:text/plain");
print $response;




// ======================================================================================
// RC4 ENCRYPTION CLASS
// Do not modify.
// ======================================================================================
/**
 * RC4Crypt 3.2
 *
 * RC4Crypt is a petite library that allows you to use RC4
 * encryption easily in PHP. It's OO and can produce outputs
 * in binary and hex.
 *
 * (C) Copyright 2006 Mukul Sabharwal [http://mjsabby.com]
 *     All Rights Reserved
 *
 * @link http://rc4crypt.devhome.org
 * @author Mukul Sabharwal <mjsabby@gmail.com>
 * @version $Id: class.rc4crypt.php,v 3.2 2006/03/10 05:47:24 mukul Exp $
 * @copyright Copyright &copy; 2006 Mukul Sabharwal
 * @license http://www.gnu.org/copyleft/gpl.html
 * @package RC4Crypt
 */

/**
 * RC4 Class
 * @package RC4Crypt
 */
class rc4crypt {
	/**
	 * The symmetric encryption function
	 *
	 * @param string $pwd Key to encrypt with (can be binary of hex)
	 * @param string $data Content to be encrypted
	 * @param bool $ispwdHex Key passed is in hexadecimal or not
	 * @access public
	 * @return string
	 */
	function encrypt ($pwd, $data, $ispwdHex = 0)
	{
		if ($ispwdHex)
			$pwd = @pack('H*', $pwd); // valid input, please!

		$key[] = '';
		$box[] = '';
		$cipher = '';

		$pwd_length = strlen($pwd);
		$data_length = strlen($data);

		for ($i = 0; $i < 256; $i++)
		{
			$key[$i] = ord($pwd[$i % $pwd_length]);
			$box[$i] = $i;
		}
		for ($j = $i = 0; $i < 256; $i++)
		{
			$j = ($j + $box[$i] + $key[$i]) % 256;
			$tmp = $box[$i];
			$box[$i] = $box[$j];
			$box[$j] = $tmp;
		}
		for ($a = $j = $i = 0; $i < $data_length; $i++)
		{
			$a = ($a + 1) % 256;
			$j = ($j + $box[$a]) % 256;
			$tmp = $box[$a];
			$box[$a] = $box[$j];
			$box[$j] = $tmp;
			$k = $box[(($box[$a] + $box[$j]) % 256)];
			$cipher .= chr(ord($data[$i]) ^ $k);
		}
		return $cipher;
	}
	/**
	 * Decryption, recall encryption
	 *
	 * @param string $pwd Key to decrypt with (can be binary of hex)
	 * @param string $data Content to be decrypted
	 * @param bool $ispwdHex Key passed is in hexadecimal or not
	 * @access public
	 * @return string
	 */
	function decrypt ($pwd, $data, $ispwdHex = 0)
	{
		return rc4crypt::encrypt($pwd, $data, $ispwdHex);
	}
}
// ======================================================================================
// END RC4 ENCRYPTION CLASS
// Do not modify.
// ======================================================================================

?>
